package com.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.product.enmu.HttpStatusEnum;
import com.product.entity.Orders;
import com.product.entity.Product;
import com.product.handler.ParameterValidationException;
import com.product.result.ApiResult;
import com.product.service.IOrderItemService;
import com.product.service.IOrdersService;
import com.product.service.IProductService;
import com.product.to.AddOrderItemTo;
import com.product.to.UpdateOrderItemTo;
import com.product.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "订单项控制器")
@RestController
@RequestMapping("/orderItem")
public class OrderItemController {
    @Autowired
    private IOrderItemService orderItemService;
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private IProductService productService;

    @Parameters({
            @Parameter(name = "size", description = "每页条数，默认10条"),
            @Parameter(name = "current", description = "分页页数，默认第一页"),
            @Parameter(name = "orderId", description = "订单id"),
            @Parameter(name = "productId", description = "产品id"),
            @Parameter(name = "orderState", description = "订单状态"),
            @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    })
    @Operation(summary = "获取所有订单项")
    @PostMapping("/all")
    private ApiResult<PageUtils> getAllOrderItems(@RequestParam(name = "size", defaultValue = "10") Integer size,
                                             @RequestParam(name = "current", defaultValue = "1") Integer current,
                                             @RequestParam(name = "orderId", required = false) Long orderId,
                                             @RequestParam(name = "productId", required = false) Long productId,
                                                  @RequestParam(name = "orderItemId", required = false) Long orderItemId,
                                             @RequestParam(name = "orderState", required = false) Integer orderState
    ) {
        return ApiResult.ok(orderItemService.getAllOrderItem(size, current, orderId, productId,orderItemId, orderState));
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "添加订单项")
    @PostMapping("/add")
    private ApiResult<Boolean> addOrderItems(@Valid @RequestBody AddOrderItemTo orderItem, BindingResult result) throws ParameterValidationException {
        if (result.hasErrors()) {
            StringBuilder errors = new StringBuilder();
            result.getAllErrors().forEach(error -> errors.append(error.getDefaultMessage()).append("\n"));
            throw new ParameterValidationException(errors.toString(), HttpStatusEnum.PARAM_ERROR.getCode());
        }
        if (ordersService.list(new LambdaQueryWrapper<Orders>().eq(Orders::getOrderId, orderItem.getOrderId())).isEmpty()) {
            return ApiResult.getResult(null, HttpStatusEnum.ERROR.getCode(), "订单id不存在");
        }
        if (productService.list(new LambdaQueryWrapper<Product>().eq(Product::getProductId, orderItem.getProductId())).isEmpty()) {
            return ApiResult.getResult(null, HttpStatusEnum.ERROR.getCode(), "产品id不存在");
        }
        orderItemService.addOrderItem(orderItem);
        return ApiResult.ok();
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "修改订单项")
    @PostMapping("/update")
    private ApiResult<Boolean> updateOrderItem(@Valid @RequestBody UpdateOrderItemTo orderItem, BindingResult result) throws ParameterValidationException {
        if (result.hasErrors()) {
            StringBuilder errors = new StringBuilder();
            result.getAllErrors().forEach(error -> errors.append(error.getDefaultMessage()).append("\n"));
            throw new ParameterValidationException(errors.toString(), HttpStatusEnum.PARAM_ERROR.getCode());
        }
        if (ordersService.list(new LambdaQueryWrapper<Orders>().eq(Orders::getOrderId, orderItem.getOrderId())).isEmpty()) {
            return ApiResult.getResult(null, HttpStatusEnum.ERROR.getCode(), "订单id不存在");
        }
        if (productService.list(new LambdaQueryWrapper<Product>().eq(Product::getProductId, orderItem.getProductId())).isEmpty()) {
            return ApiResult.getResult(null, HttpStatusEnum.ERROR.getCode(), "产品id不存在");
        }
        orderItemService.updateOrderItem(orderItem);
        return ApiResult.ok();
    }

    @Parameters({
            @Parameter(name = "orderItemId", description = "订单id"),
            @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    })
    @Operation(summary = "删除订单项")
    @PostMapping("/delete")
    private ApiResult<Boolean> deleteOrderItem(@RequestParam(name = "orderItemId") Integer orderItemId) {
        orderItemService.deleteOrderItem(orderItemId);
        return ApiResult.ok();
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "批量删除订单项")
    @PostMapping("/deletes")
    private ApiResult<Boolean> deleteOrderItems(@RequestBody List<Integer> orderItemIds) {
        orderItemService.deleteOrderItems(orderItemIds);
        return ApiResult.ok();
    }
}
